package com.da.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;

import cn.hutool.core.util.IdUtil;
import com.da.springcloud.entities.CommonResult;
import com.da.springcloud.entities.Payment;
import com.da.springcloud.myhandler.CustomerBlockHandler;
import com.da.springcloud.util.ResultUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author JokerDa.
 * @date 2021/1/16 9:15
 * Description:
 */
@RestController
public class RateLimitController {

    @GetMapping("/byResource")
    @SentinelResource(value = "byResource", blockHandler = "handleException")
    public CommonResult<Payment> byResource() {
        return ResultUtil.success("按资源名称限流测试OK", new Payment(2020L, IdUtil.simpleUUID()));
    }

    public CommonResult<Payment> handleException(BlockException blockException) {
        return ResultUtil.error(blockException.getClass().getCanonicalName() + "\t服务不可用");
    }

    @GetMapping("/rateLimit/byUrl")
    @SentinelResource(value = "byUrl")
    public CommonResult byUrl() {
        return ResultUtil.success("by url限流测试OK", new Payment(2020L, IdUtil.simpleUUID()));
    }

    //CustomerBlockHandler
    @GetMapping("/rateLimit/customerBlockHandler")
    @SentinelResource(value = "customerBlockHandler",
        blockHandlerClass = CustomerBlockHandler.class, blockHandler = "handlerException2")
    public CommonResult customerBlockHandler() {
        return ResultUtil.success("客户自定义 限流测试OK", new Payment(2020L, IdUtil.simpleUUID()));
    }
}
